package com.example.yueliang.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.yueliang.common.Result;
import com.example.yueliang.dto.HotelDto;
import com.example.yueliang.dto.HotelOrderDto;
import com.example.yueliang.pojo.Hotel;
import com.example.yueliang.pojo.HotelReservation;
import com.example.yueliang.service.impl.HotelReservationImpl;
import com.example.yueliang.service.impl.HotelRoomCategoriesImpl;
import com.example.yueliang.service.impl.HotelServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * 酒店Controller
 */

@RestController
@Slf4j
@RequestMapping("/hotel")
public class HotelController {
    @Autowired
    private HotelServiceImpl hotelService;

    @Autowired
    private HotelReservationImpl hotelReservationImpl;

    //查询推荐 的酒店信息
    @PostMapping
    public Result selectHotels() {
        LambdaQueryWrapper<Hotel> queryWrapper = new LambdaQueryWrapper<>();

        queryWrapper.like(Hotel::getHotelAddress, "推荐");
        List<Hotel> list = hotelService.list(queryWrapper);

        log.info(list.toString());
        return Result.success(list);
    }

    //根据关键词进行模糊查询
    @GetMapping("/search")
    public Result selectHotels(@RequestParam String destination) {
        LambdaQueryWrapper<Hotel> queryWrapper = new LambdaQueryWrapper<>();

        queryWrapper.like(Hotel::getHotelAddress, destination);
        List<Hotel> list = hotelService.list(queryWrapper);

        if (list.isEmpty()) {
            return Result.error("当前没有这个选项");
        }
//        log.info(list.toString());
        return Result.success(list);
    }

    //根据酒店id  查询
    @GetMapping("/{id}")
    public Result queryById(@PathVariable("id") Integer id) {

        HotelDto hotelDto = hotelService.getIdWithHotelRoom(id);

        return Result.success(hotelDto);
    }

    //添加预约订单
    @PostMapping("/reservation")
    public Result saveReservation(HttpServletRequest request, @RequestBody HotelReservation hotelReservation) {
        int uid = (int) request.getSession().getAttribute("user");

        hotelReservation.setUid(uid);
        hotelReservationImpl.save(hotelReservation);

        return Result.success("预约成功");
    }

    //查询所有预约订单
    @PutMapping("/orders")
    @Transactional
    public Result queryByUid(HttpServletRequest request) {
        LambdaQueryWrapper<HotelReservation> queryWrapper = new LambdaQueryWrapper<>();
        int uid = (int) request.getSession().getAttribute("user");
        queryWrapper.eq(HotelReservation::getUid, uid);

        List<HotelReservation> lists = hotelReservationImpl.list(queryWrapper);
        List<HotelOrderDto> orderDtoList = new ArrayList<>();
        for (HotelReservation hr : lists) {
            HotelOrderDto hotelOrderDto = new HotelOrderDto();
            Integer hid = hr.getHid();

            BeanUtils.copyProperties(hr,hotelOrderDto);

            LambdaQueryWrapper<Hotel> queryWrapper1 = new LambdaQueryWrapper<>();
            queryWrapper1.eq(Hotel::getHid,hid);
            Hotel hotel = hotelService.getOne(queryWrapper1);

            String hotelImage = hotel.getImage();
            String hotelName = hotel.getHotelName();

            hotelOrderDto.setHotelName(hotelName);
            hotelOrderDto.setHotelImage(hotelImage);

            orderDtoList.add(hotelOrderDto);
        }
        return Result.success(orderDtoList);
    }

    /**
     * 删除酒店订单
     */
    @GetMapping("/remove")
    public Result removeHotelOrder(HttpServletRequest request,@RequestParam String hotelOrderId){
        LambdaQueryWrapper<HotelReservation> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(HotelReservation::getOrderId,hotelOrderId);
        hotelReservationImpl.remove(queryWrapper);

        return Result.success("删除成功");
    }
}
